package com.wang.tasker.core;

import java.util.HashMap;

public class TaskFactory {
	
	private static HashMap<String, String> taskTypeMap = new HashMap<String, String>();
	
	public static Task getTaskByType(String taskType){
		String taskClass = taskTypeMap.get(taskType);
		if(taskClass == null)
			throw new TaskerException("Task type [" + taskType + "] is not exist!");
		return getTaskByClass(taskClass);
	}
	
	public static Task getTaskByClass(String taskClass){
		try {
			Task task = (Task)Class.forName(taskClass).newInstance();
			return task;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void addType(String typeName, String typeClass){
		taskTypeMap.put(typeName, typeClass);
	}
}
